/*
    Copyright © 2013 by Maxim Biro <nurupo.contributions@gmail.com>
    Copyright © 2014-2019 by The qTox Project Contributors

    This file is part of qTox, a Qt-based graphical interface for Tox.

    qTox is libre software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    qTox is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with qTox.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "settings.h"
#include "core/core.h"
#include "core/corefile.h"
#include "util/json.hpp"
#include "ToxDeskApp.h"


#include <list>
#include <thread>

/**
 * @var std::map<std::string, std::vector<unsigned char>> Settings::widgetSettings
 * @brief Assume all widgets have unique names
 * @warning Don't use it to save every single thing you want to save, use it
 * for some general purpose widgets, such as MainWindows or Splitters,
 * which have widget->saveX() and widget->loadX() methods.
 */

const std::string Settings::globalSettingsFile = "ToxDesk.json";
Settings* Settings::settings{nullptr};

Settings& Settings::getInstance()
{
    if (!settings)
        settings = new Settings();

    return *settings;
}

void Settings::destroyInstance()
{

}

Settings::Settings()
{
}

Settings::~Settings()
{
}

void Settings::loadGlobal() {
    std::ifstream infile(globalSettingsFile.c_str());
    if (!infile.good())
    {
        return;
    }
    // read a JSON file
    std::ifstream jsonfile(globalSettingsFile.c_str());
    nlohmann::json jsonConfig;
    jsonfile >> jsonConfig;

    if (jsonConfig.is_null()) {
        std::cout << "Failed to parse JSON document" << std::endl;
        return ;
    }
    if (jsonConfig.contains("simpleId")) {
        simpleId = jsonConfig["simpleId"].get<std::string>();
    }
}
void Settings::save2File()
{
    nlohmann::json rootObj = nlohmann::json({});
    rootObj["simpleId"] = simpleId;

     auto data = rootObj.dump();
     std::ofstream outfile;
     outfile.open(globalSettingsFile, std::ios::out);
     outfile << data;
     outfile.close();
}

std::string Settings::getVideoDev() const
{
    return "";
}

void Settings::setVideoDev(const std::string &deviceSpecifier)
{

}

bool Settings::getScreenGrabbed() const
{
    return false;
}

void Settings::setScreenGrabbed(bool value)
{

}

float Settings::getCamVideoFPS() const
{
    return 10;
}

void Settings::setCamVideoFPS(float newValue)
{

}

std::string& Settings::getSimpleId()
{
    return simpleId;
}
void Settings::setSimpleId(std::string& id)
{
    simpleId = id;
    save2File();
}




bool Settings::getEnableIPv6() const
{
    return true;
}

void Settings::setEnableIPv6(bool enable)
{

}

bool Settings::getForceTCP() const
{
    return true;
}

void Settings::setForceTCP(bool enable)
{

}

bool Settings::getEnableLanDiscovery() const
{
    return true;
}

void Settings::setEnableLanDiscovery(bool enable)
{

}

std::string Settings::getProxyAddr() const
{
    return "";
}

void Settings::setProxyAddr(const std::string &address)
{

}

ICoreSettings::ProxyType Settings::getProxyType() const
{
    return ICoreSettings::ProxyType::ptNone;
}

void Settings::setProxyType(ProxyType type)
{

}

uint16_t Settings::getProxyPort() const
{
    return 0;
}

void Settings::setProxyPort(uint16_t port)
{

}

std::string Settings::getInDev() const
{
    return "";
}

void Settings::setInDev(const std::string &deviceSpecifier)
{

}

bool Settings::getAudioInDevEnabled() const
{
    return false;
}

void Settings::setAudioInDevEnabled(bool enabled)
{

}

std::string Settings::getOutDev() const
{
    return "";
}

void Settings::setOutDev(const std::string &deviceSpecifier)
{

}

bool Settings::getAudioOutDevEnabled() const
{
    return false;
}

void Settings::setAudioOutDevEnabled(bool enabled)
{

}

double Settings::getAudioInGainDecibel() const
{
    return 0;
}

void Settings::setAudioInGainDecibel(double dB)
{

}

double Settings::getAudioThreshold() const
{
    return 0;
}

void Settings::setAudioThreshold(double percent)
{

}

int Settings::getOutVolume() const
{
    return 0;
}

int Settings::getOutVolumeMin() const
{
    return 0;
}

int Settings::getOutVolumeMax() const
{
    return 0;
}

void Settings::setOutVolume(int volume)
{

}

int Settings::getAudioBitrate() const
{
    return 0;
}

void Settings::setAudioBitrate(int bitrate)
{

}

bool Settings::getEnableTestSound() const
{
    return false;
}

void Settings::setEnableTestSound(bool newValue)
{

}
